//
//  GMArtworkInfoView.m
//  gentleman
//
//  Created by 范月成 on 2017/12/6.
//  Copyright © 2017年 fancy. All rights reserved.
//

#import "GMArtworkInfoHeader.h"
#import "GMUIHelper.h"
#import "VendorHeader.h"

@interface GMArtworkInfoHeader ()

@property (nonatomic, strong) UIImageView *thumbView;
@property (nonatomic, strong) YYLabel *titleLabel;
@property (nonatomic, strong) YYLabel *uploaderLabel;
@property (nonatomic, strong) UIButton *torrentButton;
@property (nonatomic, strong) YYLabel *starLabel;
@property (nonatomic, strong) YYLabel *catgeoryLabel;
@property (nonatomic, strong) UIButton *readButton;
@property (nonatomic, strong) UIButton *downloadButton;
@property (nonatomic, assign) BOOL isDownload;

@end

@implementation GMArtworkInfoHeader

- (instancetype)init {
    if (self = [super init]) {
        [self addSubviews];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubviews];
    }
    return self;
}

- (void)addSubviews {
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.thumbView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.uploaderLabel];
    [self addSubview:self.torrentButton];
    [self addSubview:self.starLabel];
    [self addSubview:self.catgeoryLabel];
    [self addSubview:self.downloadButton];
    [self addSubview:self.readButton];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.thumbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(6);
        make.bottom.equalTo(self).offset(-6);
        make.left.equalTo(self).offset(12);
        make.width.mas_equalTo(self.mas_height).multipliedBy(0.618);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(6);
        make.left.equalTo(self.thumbView.mas_right).offset(6);
        make.right.equalTo(self).offset(-12);
    }];
    
    [self.uploaderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(6);
        make.left.equalTo(self.thumbView.mas_right).offset(6);
    }];
    
    [self.torrentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.uploaderLabel.mas_bottom).offset(6);
        make.left.equalTo(self.thumbView.mas_right).offset(6);
        make.right.equalTo(self).offset(-12);
        make.height.mas_equalTo(20);
    }];
    
    [self.starLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.catgeoryLabel.mas_top).offset(-6);
        make.left.equalTo(self.thumbView.mas_right).offset(6);
        make.height.mas_equalTo(20);
    }];
    
    [self.catgeoryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-6);
        make.left.equalTo(self.thumbView.mas_right).offset(6);
        make.height.mas_equalTo(20);
    }];
    
    [self.downloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-52);
        make.right.equalTo(self.mas_right).offset(-6);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(80);
    }];
    
    [self.readButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-6);
        make.right.equalTo(self.mas_right).offset(-6);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(80);
    }];
}

- (void)setArtwork:(GMArtwork *)artwork {
    GMArtwork *art = [GMArtwork objectForId:@(artwork.gid)];
    _artwork = art?art:artwork;
    [self.thumbView sd_setImageWithURL:[NSURL URLWithString:artwork.thumb]
                      placeholderImage:[UIImage placeholderImage]];
    self.titleLabel.text = _artwork.title;
    if (_artwork.isDownload) {
        self.isDownload = YES;
        self.downloadButton.enabled = YES;
        [self.downloadButton setTitle:@"已下载" forState:UIControlStateNormal];
    }
    self.uploaderLabel.text = _artwork.uploader;
    self.starLabel.text = [NSString stringWithFormat:@"评分:%.1f 星", _artwork.rating];
    self.catgeoryLabel.text = _artwork.category;
    self.catgeoryLabel.backgroundColor = [UIColor colorWithCategory:_artwork.category];
    self.torrentButton.hidden = !_artwork.torrentcount;
}

- (void)setHandelButtonsEnable:(BOOL)enable {
    self.downloadButton.enabled = enable;
    self.readButton.enabled = enable;
}

#pragma mark - action
- (void)getTorrent:(UIButton *)sender {
    !self.torrentBlock?:self.torrentBlock();
}

- (void)read:(UIButton *)sender {
    !self.readBlock?:self.readBlock();
}

- (void)download:(UIButton *)sender {
    !self.downloadBlock?:self.downloadBlock(sender, self.isDownload);
}

#pragma mark - getter
- (UIImageView *)thumbView {
    if (!_thumbView) {
        _thumbView = [[UIImageView alloc] init];
        _thumbView.contentMode = UIViewContentModeScaleAspectFill;
        _thumbView.clipsToBounds = YES;
        _thumbView.userInteractionEnabled = YES;
        @weakify(self)
        [_thumbView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            @strongify(self)
            !self.thumbTapBlock?:self.thumbTapBlock(self.thumbView);
        }]];
    }
    return _thumbView;
}

- (YYLabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [GMUIHelper generalTitleLabel];
        _titleLabel.numberOfLines = 2;
    }
    return _titleLabel;
}

- (YYLabel *)uploaderLabel {
    if (!_uploaderLabel) {
        _uploaderLabel = [GMUIHelper generalSubTitleLabel];
    }
    return _uploaderLabel;
}

- (YYLabel *)catgeoryLabel {
    if (!_catgeoryLabel) {
        _catgeoryLabel = [GMUIHelper labelWithFont:kSystemFont(14) textColor:[UIColor whiteColor]];
        _catgeoryLabel.textContainerInset = UIEdgeInsetsMake(0, 5, 0, 5);
        @weakify(self)
        _catgeoryLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            @strongify(self)
            !self.categoryTapBlock?:self.categoryTapBlock(text.string);
        };
    }
    return _catgeoryLabel;
}

- (UIButton *)torrentButton {
    if (!_torrentButton) {
        _torrentButton = [GMUIHelper buttonWithFont:kSystemFont(13) titleColor:[UIColor themeGray]];
        [_torrentButton setTitle:@"种子信息  >" forState:UIControlStateNormal];
        _torrentButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_torrentButton addTarget:self action:@selector(getTorrent:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _torrentButton;
}

- (UIButton *)readButton {
    if (!_readButton) {
        _readButton = [GMUIHelper buttonWithFont:kSystemFont(16) titleColor:[UIColor whiteColor]];
        [_readButton setBackgroundImage:[UIImage imageWithColor:[UIColor themeGray]] forState:UIControlStateDisabled];
        [_readButton setBackgroundImage:[UIImage imageWithColor:[UIColor themeBlue]] forState:UIControlStateNormal];
        [_readButton setTitle:@"阅读" forState:UIControlStateNormal];
        _readButton.enabled = NO;
        _readButton.layer.cornerRadius = 4;
        _readButton.layer.masksToBounds = YES;
        _readButton.layer.borderWidth = .5f;
        _readButton.layer.borderColor = [UIColor themeLightgory].CGColor;
        [_readButton addTarget:self action:@selector(read:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _readButton;
}

- (UIButton *)downloadButton {
    if (!_downloadButton) {
        _downloadButton = [GMUIHelper buttonWithFont:kSystemFont(16) titleColor:[UIColor whiteColor]];
        [_downloadButton setBackgroundImage:[UIImage imageWithColor:[UIColor themeGray]] forState:UIControlStateDisabled];
        [_downloadButton setBackgroundImage:[UIImage imageWithColor:[UIColor themePink]] forState:UIControlStateNormal];
        [_downloadButton setTitle:@"下载" forState:UIControlStateNormal];
        _downloadButton.enabled = NO;
        _downloadButton.layer.cornerRadius = 4;
        _downloadButton.layer.masksToBounds = YES;
        _downloadButton.layer.borderWidth = .5f;
        _downloadButton.layer.borderColor = [UIColor themeLightgory].CGColor;
        [_downloadButton addTarget:self action:@selector(download:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _downloadButton;
}

- (YYLabel *)starLabel {
    if (!_starLabel) {
        _starLabel = [GMUIHelper generalSubTitleLabel];
    }
    return _starLabel;
}
 
@end
